﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VSWClient.Utility
{
    public class TcpMessageHelper
    {
        public string CommandId { private set; get; }

        /// <summary>
        /// Time on server handled
        /// </summary>
        public DateTime Time { private set; get; }

        public string[] Parameter { private set; get; }

        /// <summary>
        /// This is a bad reply
        /// </summary>
        public bool Vaild { private set; get; }

        /// <summary>
        /// Command excute scuess on server
        /// </summary>
        public bool Status { private set; get; }

        public int ParameterLength { private set; get; }

        public TcpMessageHelper(string message)
        {
            string[] recieveMessage = message.Split(';');
            if (recieveMessage.Length<3)
            {
                Vaild = false;
                Status = false;
                ParameterLength = 0;
            }
            else
            {
                try
                {
                    CommandId = recieveMessage[0];
                    Time= Convert.ToDateTime(recieveMessage[1]);
                    Status = recieveMessage[2] == "1" ? true:false;
                    int length = recieveMessage.Length - 3;

                    if (length > 0)
                    {
                        string[] body = new string[length];
                        Array.Copy(recieveMessage, 3, body, 0, length);
                        Parameter = body;
                        ParameterLength = length;
                    }
                    else
                        ParameterLength = 0;
                    Vaild = true;
                }
                catch
                {				
                    Vaild = false;
                    Status = false;
                    ParameterLength = 0;
                }
            }
        }
    }
}
